package LinkRetriever;
import java.util.ArrayList;
import General.*;
import java.io.File;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Manuel
 */
public class MapManager extends Thread{

    private MapParser parser;
    private String mapURL;
    private String fileName;
    private ArrayList<String> maps;
    private int noOfLinks;


    public MapManager(String map, String f){
            mapURL = map;
            fileName = f;
            maps = new ArrayList();
            parser = new MapParser();
    }

    public static Thread crawlSiteMaps(String mapURL, String file){
        MapManager m = new MapManager(mapURL, file);
        m.start();
        return m;
    }


    public void run(){
        System.out.println("Crawling " + this.mapURL + " ...");
        String map = RequestMaker.getMap(mapURL);
        if(!map.equals("")){
            int linkCount = 0;
            ArrayList<String> links = parser.parse(map);
            for(int i = 0;i<links.size();i++){
                Utilities.writeToFile(links.get(i), fileName,true);
                linkCount += links.get(i).replaceAll("[^;]", "").length();
            }
            System.out.println("Finished. Gathered "+ linkCount + " links.");
            Utilities.cleanMemory();
        }
    }



}
